#!/bin/bash

# YQuote C++ Backend Build Script

set -e  # 遇到错误时退出

echo "🚀 开始构建 YQuote C++ 后端..."

# 检查依赖
echo "📋 检查依赖..."
if ! command -v cmake &> /dev/null; then
    echo "❌ 错误: 未找到 cmake，请先安装 cmake"
    exit 1
fi

if ! pkg-config --exists openssl; then
    echo "❌ 错误: 未找到 OpenSSL，请先安装 OpenSSL 开发库"
    exit 1
fi

# 创建构建目录
echo "📁 创建构建目录..."
mkdir -p build
cd build

# 配置构建类型
BUILD_TYPE=${1:-Release}
echo "🔧 配置构建类型: $BUILD_TYPE"

# 运行 cmake
echo "⚙️ 运行 cmake..."
cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..

# 编译
echo "🔨 开始编译..."
make -j$(nproc 2>/dev/null || echo 4)

echo "✅ 构建完成！"
echo "📍 可执行文件位置: $(pwd)/quote_server"
echo ""
echo "🚀 运行服务器:"
echo "   ./quote_server"
echo ""
echo "📖 查看帮助:"
echo "   ./quote_server --help" 